
import HomePage from '@/pages/home.vue'
import createDemoPageComponent from '@/components/createDemoPageComponent'
import demoModules from '@/router/demoModules'
function getBaseFileNameFromPath(path) {
  const lastSlashIndex = path.lastIndexOf('/');
  const fileNameWithoutExt = path.substring(lastSlashIndex + 1);
  const dotIndex = fileNameWithoutExt.lastIndexOf('.');
  return dotIndex === -1 ? fileNameWithoutExt : fileNameWithoutExt.substring(0, dotIndex);
}

const filesPath = Object.keys(demoModules)
const filesName = filesPath.map(path=>getBaseFileNameFromPath(path))
const filesMap = filesName.map(item=>{
  let name = item.split('-')
  if(name.length !== 3){
    throw new Errow( '示例文件必须在demos文件夹下 文件名格式为 ui-dialog-demo1 必须是3段' )
  }
  return {
    group: name[0],
    comp: name[1],
    demo: name[2]
  }
})



let groups = []
let comps = []
let demoPageNames = []


filesMap.forEach(item => {
  groups.push(item.group)
  comps.push(item.comp)
  demoPageNames.push(`${item.group}-${item.comp}`)
  // 去重
  groups = [...new Set(groups)]
  comps = [...new Set(comps)]
  // 去重 页面分组
  demoPageNames = [...new Set(demoPageNames)]
});

// console.log("🚀 ~ groups:", groups)// ["ui"]
// console.log("🚀 ~ comps:", comps)// ['dialog']
// console.log("🚀 ~ demoPageNames:", demoPageNames)// ['ui-dialog']
const groupMap = {
  ui: 'UI界面'
}
// 自动生成路由 文件必须在demos文件夹下 文件名格式为 'ui-dialog-demo1' 必须是3段
const routes = demoPageNames.map(item=>{
  const [group,comp]= item.split('-')
  const demos = filesMap.filter(obj=>{
    return obj.group === group && obj.comp === comp
  })
  const demosOption = demos.map(item=>{
    return {
      group:item.group,
      comp:item.comp,
      demo:item.demo,
    }
  })
  return { 
    path: `/${group}/${comp}`,
    title: `${comp}`,
    component: createDemoPageComponent({
      group,
      comp,
      demosOption,
    })
  }
})
// 静态示例
// const routes = [
//   { 
//     path: '/ui/dialog',
//     title: '弹窗',
//     component: createDemoPageComponent({
//       title:'dialogxx',
//       desc:'弹窗描述',
//       demosOption:[{
//         title:'123',
//         desc:'32423',
//         path:'ui-dialog-demo1'
//       }]
//     })
//   },

// ]

const allRoutes = [
{
  path:'/',
  title:'首页',
  component: HomePage
}
].concat(routes)
export default allRoutes